<?php

class Act_sales extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model( 'msales' );
		$this->load->model( 'mglobal' );
		$this->load->library('enc');
	}
	
	public function getById( $id ) {
		if( isset( $id ) )
			echo json_encode( $this->msales->getById( $id ) );
	}
    
    public function index()
    {
        redirect('index.php/master/salesOrder');
    }
	
	public function create() {
		if( !empty( $_POST ) ) {
			echo $this->mpage->create();
			//echo 'New user created successfully!';
		}
	}
	
	// untuk manage data yang ditampilkan pada grid view
	public function read($type = '') {
		//echo json_encode( $this->msales->getAll() );
		//edit by alfin akhret
		$cluster_id = $this->mglobal->user_cluster($this->session->userdata("username"));
		if($this->session->userdata('is_self_service') == 1)
		{
			$result = $this->msales->getAllCurrentUser($cluster_id);				
		}
		else
		{
			if($type == 'history')
			{
				$result = $this->msales->getAll($cluster_id, 'history');
			}
			else
			{
				$result = $this->msales->getAll($cluster_id);
			}				
		}


     //    //encode
     //  for($i=0;$i<count($result);$i++){
    	// $result[$i]->so_id = $this->enc->fogit($result[$i]->so_id);
    	// $result[$i]->territory_id = $this->enc->fogit($result[$i]->territory_id);
    	// }

		// encode
        for($i=0;$i<count($result['aaData']);$i++){
    		$result['aaData'][$i][0] = $this->enc->fogit($result['aaData'][$i][0]);
    	}
        
      //penambahan nomor statis untuk grid SSP
        $start=$_GET['iDisplayStart'];
        $result['iDisplayStart']=$start;

      echo json_encode($result);
	}
	
	public function update() {
		if( !empty( $_POST ) ) {
			$this->mpage->update();
			echo 'Record updated successfully!';
		}
	}
	
	public function delete( $id = null ) {
		if( is_null( $id ) ) {
			echo 'ERROR: Id not provided.';
			return;
		}
		$this->mpage->delete( $id );
		echo 'Records deleted successfully';
	}
} //end class
